$(document).ready(function() {
    Highcharts.setOptions({
        global: {
            useUTC: false
        },
        colors:['#F0382B','#28A1F6','#27D834']
    });
    j = 0;
    LineSeries = new Array()
    $('#ping').click(function () {
        toip = $("#Local").val();
        alert(toip);
        alert(LineSeries.length);
        status = 1;
        if(LineSeries.length != 0){
            for(i = 0;i < LineSeries.length;i++){
                if(LineSeries[i] == toip){
                    alert("该IP已经使用");
                    status = 0;
                    break;
                }
            }
            if(status == 1){
                LineSeries[j] = toip;
                alert(LineSeries[j]);
                alert("222222");
                chart.addSeries({
                    name:toip,
                    data:[]
                });
                alert("线条生成成功");
                j = j + 1;
            }
        }
        else {
            LineSeries[j] = toip;
            alert(LineSeries[j]);
            alert(chart.series.length);
            chart.addSeries({
                name:toip,
                data:[0]
            });
            alert("11111111");
            j = j + 1;
        }
    });
    var chart;
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'ct2',  //指向的div的id属性
            // backgroundColor: '#808080',
            type: 'spline',
            marginRight: 10,
            events: {
                load: function() {

                    // set up the updating of the chart each second
                    //alert("1");
                    setInterval(function() {
                        alert(j);
                        YY = new Array();
                        for(num = 0;num < this.series.length;num++){
                            YY[num] = 0;
                        }
                        var x = (new Date()).getTime(); // current time
                        //alert(series1.name);
                        for(num = 0;num < this.series.length;num++){
                            getYY(YY[num],series[num].name);
                            alert(YY[num]);
                        }
                        //alert("5");
                        // addPoint 第二个参数表示是否对图表进行重绘，当有多个参数是，需先关闭重绘，
                        // 等操作完毕后再调 chart.redraw();
                        for(num = 0;num < this.series.length;num++){
                            series[num].addPoint([x, YY[num]], false, true);
                        }
                        chart.redraw();
                    }, 1200);
                }
            }
        },
        title: {
            text: '<b>综合应用设计</b>'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150  //x轴上的间隔
        },
        yAxis: [{
            title: {
                text: '单位：毫秒(ms)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#000000'
            }]
        }],
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: true
        },
        exporting: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        series: []
    });
});
function getYY(theObject,name){
    var params = {};
    params.ipAddress = name;

        $.ajax({
            type: "post",
            url: "http://localhost:8080/getPingInfo.action",
            data:params,
            dataType: "json",

            success: function (res, status) {
                theObject = parseFloat(res.pingtime);
                if(res.Code == "null")
                    theObject = 0;
            },
            error: function (res, status, xhr) {
                theObject = 0;
            }
        });
}